#ifndef __AUTOFREE_H__
#define __AUTOFREE_H__
#include <stdlib.h>
#include <string.h>
#include <wchar.h>

template <typename T>
class AutoFree
{
public:
    AutoFree(size_t len)
    {
        Init();
        if (len > 0)
        {
            m_buff = new T[len];
            m_len = len;

            if (m_buff)
            {
                memset((void *)m_buff, 0, len * sizeof(T));
            }
        }
    }
    AutoFree()
    {
        Init();
    }
    AutoFree(T *addr)
    {
        Init();
        m_buff = addr;
    }
    void Init()
    {
        m_buff = NULL;
        m_len = 0;
    }
    ~AutoFree()
    {
        if (m_buff)
        {
            delete[] m_buff;
        }
        m_buff = NULL;
    }
    inline T *operator->()
    {
        return m_buff;
    }
    inline T *operator=(T *addr)
    {
        Init();
        m_buff = addr;
        return m_buff;
    }
    inline operator T *()
    {
        return m_buff;
    }

public:
    size_t m_len;
    T *m_buff;
};

#endif // !__AUTOFREE_H__
